<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport"
			content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../css/common.css" media="all">

	</head>
	<body>

		<div class="layui-fluid">
			<div class="layui-row" id="view">
				<form class="layui-form" wid100 lay-filter="test_detail">
					<div class="layui-card">
						<div class="layui-card-body">
							<div class="layui-form-item">
								<label class="layui-form-label">需求状态</label>
								<div class="layui-input-block on_off"></div>
							</div>
							<!--<div class="layui-form-item">
								<label class="layui-form-label">需求类型</label>
								<div class="layui-input-block goods_type"></div>
							</div>-->
							<div class="layui-form-item">
								<label class="layui-form-label">需求内容</label>
								<div class="layui-input-block">
									<input type="text" name="content" placeholder="请输入需求内容" autocomplete="off"
										class="layui-input" lay-verify="required">
								</div>
							</div>
							<div class="layui-form-item">
								<div class="layui-block">
									<label class="layui-form-label">服务地址</label>
									<div class="layui-input-block">
										<input type="text" name="address" placeholder="请输入服务地址" autocomplete="off"
											class="layui-input" lay-verify="required">
									</div>
								</div>
							</div>
							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">预算</label>
									<div class="layui-input-inline">
										<input type="text" name="budget" placeholder="请输入预算" autocomplete="off"
											class="layui-input" lay-verify="">
									</div>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">需求详情图</label>
								<div class="layui-input-block">
									<button type="button" class="layui-btn layui-btn-normal"
										onclick="choose(1)">上传图片</button>
									<blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
										<ul class="show_img_ul" id="img"></ul>
									</blockquote>
								</div>
							</div>
						</div>
					</div>

					<div class="layui-form-item layui-layout-admin">
						<div class="layui-input-block">
							<div class="layui-footer" style="left: 0;">
								<!-- <button type="reset" class="layui-btn layui-btn-primary">重置</button> -->
								<button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
		<script src="../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../js/jquery.min.js"></script>
		<script src="../../../js/jquery.jqprint-0.3.js"></script>
		<script src="../../../js/ace/ace.js"></script>
		<script src="../../../js/common.js"></script>
		<!--<script src="https://raw.githack.com/hnzzmsf/layui-formSelects/master/dist/formSelects-v4.min.js" type="text/javascript" charset="utf-8"></script>-->
		<script src="../../../layuiadmin/modules/formSelects-v4.js"></script>
		<script>
			var _data = {
				id: getParam('id'),
			}

			var request_data = []

			var str = '';

			function child(data, index) {
				$(" input[name=id]").val(data.id);
			}
			layui.config({
				base: '../../../layuiadmin/' //静态资源所在路径
			}).extend({
				tinymce: '../tinymce/tinymce',
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'laytpl', 'laydate', 'layedit', 'upload', 'tinymce', 'util'], function() {
				var $ = layui.$,
					admin = layui.admin,
					upload = layui.upload,
					laytpl = layui.laytpl,
					laydate = layui.laydate,
					util = layui.util,
					tinymce = layui.tinymce,
					form = layui.form;

				on_off_show('radio');
				goods_type_show('radio')
				var img_arr1 = [];

				if (_data.id) {
					initdata();
					$("#spec").show()
				} else {
					$("#spec").hide()
				}

				util.event('lay-event', {
					preveiw: function() {
						layer.open({
							type: 2,
							title: '预览',
							shadeClose: false,
							shade: 0.5,
							maxmin: true, //开启最大化最小化按钮
							area: ['60%', '80%'],
							content: 'pre_content.html',
							end: function() {},
							success: function(layero, index) {
								var body = layer.getChildFrame('body', index);
								body.find(".title").html($('input[name=title]').val());
								body.find(".showcontent").html(edit.getContent());
							}
						});
					},
				});

				//监听提交
				form.on('submit(demo1)', function(data) {

					var data = data.field;
					_data = Object.assign(_data, data);
					_data.img_url = img_arr1.join(',');
					var lock = false
					layer.confirm('确定要提交数据？', function() {
						if (!lock) {
							lock = true
							loading();
							if (_data.type == 0) {
								_data.test_typ == '';
							}
							if (_data.id) {
								submitdata(server.goods_requirement_edit);
								console.log(_data.img_url);
							} else {
								submitdata(server.goods_requirement_insert);
							}
						}
					}, function() {
						layer.msg('取消操作');
					});
					return false;
				});

				function submitdata(_url) {
					$.post(_url, _data, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								layer.alert('操作成功', function() {
									parent.layer.closeAll();
									parent.layui.table.reload('test-table-operate');
								});
							} else {
								closeloading();
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
				window.choose = function(data) {
					layer.open({
						type: 2,
						title: '上传图片',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['700px', '400px'],
						content: 'up_report.html?type=' + data,
						end: function() {},
						success: function() {}
					});
				}
				window.getimg = function(data, type) {
					console.log(data);
					if (data) {
						switch (type) {
							case '1':
								createli(data, 'img', type)
								break;
							default:
								break;
						}
					}
				}
				window.delimg = function(i, type, _self) {
					var _type = String(type)
					var lock = false
					layer.confirm('确定要删除图片？', function(index) {
						if (!lock) {
							lock = true
							switch (_type) {
								case '1':
									img_arr1.splice(i, 1)
									break;
								default:
									break;
							}
							$(_self).parent().remove();
							layer.close(index)
						}
					}, function() {
						layer.msg('取消操作');
					});
					// console.log(i);
					// console.log(type);
				}

				function createli(data, dom, type) {
					// console.log(data);
					var lihtml = '';
					switch (type) {
						case '1':
							if (data.indexOf(',') == -1) {
								img_arr1.push(data)
							} else {
								for (var i = 0; i < data.split(',').length; i++) {
									img_arr1.push(data.split(',')[i])
								}
							}
							for (var i = 0; i < img_arr1.length; i++) {
								lihtml += '<li class="show_img_li">' +
									'<img src=' + img_arr1[i] + ' >' +
									'<p onclick="delimg(' + i + ',1,this)">删除</p>' +
									'</li>'
							}
							break;
						default:
							break;
					}
					$('#' + dom).html(lihtml);
				}

				function initdata() {
					loading()
					$.get(server.goods_requirement_detail, _data, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								var data = res.goods_requirement
								$("input[name=status][value=" + data.data.status + "]").attr("checked", true);
								$("input[name=type][value=" + data.data.type + "]").attr("checked", true);
								data.data.img_url ? createli(data.data.img_url, 'img', '1') : ''
								console.log(data.data.spec)
								$(".on_off input[value=" + data.data.status + "]").attr("checked", true)
								form.val("test_detail", data.data);
								form.render();
								edit.setContent(data.data.content)
								// layedit.setContent(indexbuild,data.data.content);
								closeloading()
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
					closeloading()
				}
				$(".spec_add").bind("click", function(e) {
					console.log(e.currentTarget) //当前 元素
					var current = e.currentTarget //当前节点
					var current_parent = e.currentTarget.parentNode //当前父节点
					var deepList = current_parent.cloneNode(true); //拷贝父节点 

					current_parent.parentNode.insertBefore(deepList, current_parent) // 插入到 当前父节点 上面
					current_parent.previousElementSibling.removeChild($(".spec_add")[0]) // 

					console.log(e.currentTarget.parentNode.parentNode.childNodes)
					var nodes_list = e.currentTarget.parentNode.parentNode.childNodes




					console.log(get_spec($(".layui-field-box")[0].childNodes, _data))
				})

				$(".spec_del").bind("click", function(e) {
					console.log(e.currentTarget) //当前 元素
					var current = e.currentTarget //当前节点
					var current_parent = e.currentTarget.parentNode //当前父节点
					current_parent.parentNode.remove(current_parent)



					console.log(get_spec($(".layui-field-box")[0].childNodes, _data))
				})

				function get_spec(nodes_list, _data) {
					var obj = {}
					str = 'obj.id=' + _data.id
					for (var i = 0; i < nodes_list.length; i++) {
						var node = nodes_list[i]
						var spec_k = $(node).find('input[name=spec_k]').val(),
							spec_v = $(node).find('input[name=spec_v]').val();
						if (spec_k) {
							str += ",obj." + spec_k + "='" + spec_v + "'"
							eval(str);
						}
					}
					return JSON.stringify(obj)
				}
			});
		</script>
	</body>
</html>
